<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(null, true);
	}
	
	public function candidate($candidate_id = null, $candidate_name = null, $subscription_id = null)
	{
		$this->lang->load('candidate/profile', $this->session->userdata['user_lang']);
		$this->lang->load('candidate/personal', $this->session->userdata['user_lang']);
		$this->lang->load('candidate/resume', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_model', 'candidate');
		$this->load->model('Subscription_model', 'subscription');
		$this->load->helper('date');
		
		if (is_numeric($subscription_id))
		{
			$this->subscription->check_view_profile($subscription_id);
		}
		
		$this->get_subscription_menu($data, $subscription_id, $candidate_id);
		
		$data['main_content'] = 'candidate/profile/resume';
        $data['userdata'] = $this->session->userdata;
        
        $data['candidate'] = $this->candidate->get_complete_profile(array('idcandidate' => $candidate_id));
        $data['profile_image'] = $data['candidate']->image;
        
        $start_date  = strtotime($data['candidate']->birthyear.'/'.$data['candidate']->birthmonth.'/'.$data['candidate']->birthday.' 12:00:00');
        $date_diff = explode(' ', timespan($start_date, time()));
        $data['candidate']->age = $date_diff[0] . ' ' . $this->lang->line('profile_age');
        $data['candidate']->birthday = format_date($data['candidate']->birthyear.'-'.$data['candidate']->birthmonth.'-'.$data['candidate']->birthday, true);
        
        $data['grid_phone'] = $this->get_candidate_phone($candidate_id);
        $data['grid_address'] = $this->get_candidate_address($candidate_id);
        $data['grid_site'] = $this->get_candidate_site($candidate_id);
        $data['grid_language'] = $this->get_candidate_language($candidate_id);
        $data['grid_course'] = $this->get_candidate_course($candidate_id);
        $data['grid_experience'] = $this->get_candidate_experience($candidate_id);
		
        $data['page_title'] = $data['candidate']->firstname . ' ' . $data['candidate']->lastname;
        $this->load->view('template-candidate-profile', $data);
	}
	
	private function get_candidate_phone($candidate_id)
	{
		$this->lang->load('candidate/phone', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_phone_model', 'phone');
		
		$phone_list = $this->phone->get(array('idcandidate' => $candidate_id));
		$grid_phone = array();
        
        foreach ($phone_list->result() as $row)
		{
			$grid_phone[] = '<div class="row"><span>(' . $row->region . ') ' . $row->number . '</span></div>';
		}
		
		if (!$grid_phone) {
			 return '<center>'.$this->lang->line('phone_noresults').'</center>';
		} else {
			 return implode('<hr/>', $grid_phone);
		}
	}
	
	private function get_candidate_address($candidate_id)
	{
		$this->lang->load('candidate/address', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_address_model', 'address');
		
		$address_list = $this->address->get(array('idcandidate' => $candidate_id));
				
		$grid_address = array();
        
        foreach ($address_list->result() as $row)
		{
			$grid_address[] = '<div class="row"><span><span>' . $row->address . ', '.$row->number . ($row->additional != '' ? (' ('.$row->additional.')') : '') . '</span>'
			.' - <span class="gdesc">'.$row->neighborhood.' - ' . $row->city .' / '.$row->stateshortname . ' (' . $row->countryname . ')' 
			. '</span></span></div>';
		}
		
		if (!$grid_address) {
			 return '<center>'.$this->lang->line('address_noresults').'</center>';
		} else {
			 return implode('<hr/>', $grid_address);
		}
	}
	
	private function get_candidate_site($candidate_id)
	{
		$this->lang->load('candidate/site', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_site_model', 'site');
		
		$site_list = $this->site->get(array('idcandidate' => $candidate_id));
		$grid_site = array();
        
        foreach ($site_list->result() as $row)
		{
			$url_link = $row->url;
			
			if (!parse_url($row->url, PHP_URL_SCHEME)) {
				$url_link = 'http://' . $row->url;
			}
			
			$grid_site[] = '<div class="row"><span><a href="'.$url_link.'">' . $row->url . '</a>' . (isset($row->typename) ? (' (' . $row->typename . ')') : '')
			. ($row->description ? ' - <span class="gdesc">'.$row->description.'</span>' : '')
			. '</span></div>';
		}
		
		if (!$grid_site) {
			 return '<center>'.$this->lang->line('site_noresults').'</center>';
		} else {
			 return implode('<hr/>', $grid_site);
		}
	}
	
	private function get_candidate_language($candidate_id)
	{
		$this->lang->load('candidate/language', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_language_model', 'language');
		
		$language_list = $this->language->get(array('idcandidate' => $candidate_id));
		       	
		$grid_language = array();
        
        foreach ($language_list->result() as $row)
		{
			$grid_language[] = '<div class="row"><span><span>'.$row->languagename.' ('.$row->levelname.')</span></span></div>';
		}
		
		if (!$grid_language) {
			 return '<center>'.$this->lang->line('language_noresults').'</center>';
		} else {
			 return implode('<hr/>', $grid_language);
		}
	}
	
	private function get_candidate_course($candidate_id)
	{
		$this->lang->load('candidate/course', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_course_model', 'course');
		
		$course_list = $this->course->get(array('idcandidate' => $candidate_id));
				
		$grid_course = array();
        
        foreach ($course_list->result() as $row)
		{
			$grid_course[] = '<div class="row"><span><span class="bold">' . $row->levelname . ' - ' . $row->typename . ' (' . $row->statusname . ')</span>'
			. '<br/><span class="gdesc">'.$row->institution.' - ' . format_date($row->startdate, true) . ($row->finishdate ? ' &agrave; ' . format_date($row->finishdate, true) : '') .'
			<br/>'.($row->statename != '' ? $row->statename.' / ' : '').$row->countryname.'</span>
			</span></div>';
		}
		
		if (!$grid_course) {
			 return '<center>'.$this->lang->line('course_noresults').'</center>';
		} else {
			 return implode('<hr/>', $grid_course);
		}
	}
	
	private function get_candidate_experience($candidate_id)
	{
		$this->lang->load('candidate/experience', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_experience_model', 'experience');
		
		$experience_list = $this->experience->get(array('idcandidate' => $candidate_id));
				
		$grid_experience = array();
        
        foreach ($experience_list->result() as $row)
		{
			$grid_experience[] = '<div class="row"><span><span class="bold">' . $row->title . ' ('.$row->hierarchy.')</span>'
			. '<br/><span class="gdesc">' . $row->specialty . ' ('.$row->category.')<br/>'.$row->company.' - ' . format_date($row->startdate, true)
			. ($row->finishdate ? ' &agrave; ' . format_date($row->finishdate, true) : ($row->idnow == 'Y' ? (' ' . $this->lang->line('experience_sofar')) : ''))
			.'<br/>'.($row->statename != '' ? $row->statename.' / ' : '') . $row->countryname.'<br/><br/>' 
			. $row->description . '</span></span></div>';
		}
		
		if (!$grid_experience) {
			 return '<center>'.$this->lang->line('experience_noresults').'</center>';
		} else {
			 return implode('<hr/>', $grid_experience);
		}
	}
	
	public function company($company_id = null, $company_name = null, $subscription_id = null)
	{
		$this->lang->load('company/profile', $this->session->userdata['user_lang']);
		$this->lang->load('company/institutional', $this->session->userdata['user_lang']);
		$this->load->model('Company_model', 'company');
		$this->load->model('Subscription_model', 'subscription');
		$this->load->helper('date');
	
// 		if (is_numeric($subscription_id))
// 		{
// 			$this->subscription->check_view_profile($subscription_id);
// 		}
	
// 		$this->get_subscription_menu($data, $subscription_id, $candidate_id);
	
		$data['main_content'] = 'company/profile/main';
		$data['userdata'] = $this->session->userdata;
	
		$data['company'] = $this->company->get_complete_profile(array('idcompany' => $company_id));
		$data['profile_image'] = $data['company']->image;
	
		$start_date  = strtotime($data['company']->birthyear.'/'.$data['company']->birthmonth.'/'.$data['company']->birthday.' 12:00:00');
		$date_diff = explode(' ', timespan($start_date, time()));
		$data['company']->age = $date_diff[0] . ' ' . $this->lang->line('profile_age');
		$data['company']->birthday = format_date($data['company']->birthyear.'-'.$data['company']->birthmonth.'-'.$data['company']->birthday, true);
	
 		$data['grid_phone'] = $this->get_company_phone($company_id);
 		$data['grid_address'] = $this->get_company_address($company_id);
 		$data['grid_site'] = $this->get_company_site($company_id);
	
		$data['page_title'] = $data['company']->name;
		$this->load->view('template-company-profile', $data);
	}
	
	private function get_company_phone($company_id)
	{
		$this->lang->load('company/phone', $this->session->userdata['user_lang']);
		$this->load->model('Company_phone_model', 'phone');
	
		$phone_list = $this->phone->get(array('idcompany' => $company_id));
		$grid_phone = array();
	
		foreach ($phone_list->result() as $row)
		{
			$grid_phone[] = '<div class="row"><span>(' . $row->region . ') ' . $row->number . '</span></div>';
		}
	
		if (!$grid_phone) {
			return '<center>'.$this->lang->line('phone_noresults').'</center>';
		} else {
			return implode('<hr/>', $grid_phone);
		}
	}
	
	private function get_company_address($company_id)
	{
		$this->lang->load('company/address', $this->session->userdata['user_lang']);
		$this->load->model('Company_address_model', 'address');
	
		$address_list = $this->address->get(array('idcompany' => $company_id));
	
		$grid_address = array();
	
		foreach ($address_list->result() as $row)
		{
			$grid_address[] = '<div class="row"><span><span>' . $row->address . ', '.$row->number . ($row->additional != '' ? (' ('.$row->additional.')') : '') . '</span>'
			.' - <span class="gdesc">'.$row->neighborhood.' - ' . $row->city .' / '.$row->stateshortname . ' (' . $row->countryname . ')'
			. '</span></span></div>';
		}
	
		if (!$grid_address) {
			return '<center>'.$this->lang->line('address_noresults').'</center>';
		} else {
			return implode('<hr/>', $grid_address);
		}
	}
	
	private function get_company_site($company_id)
	{
		$this->lang->load('company/site', $this->session->userdata['user_lang']);
		$this->load->model('Company_site_model', 'site');
	
		$site_list = $this->site->get(array('idcompany' => $company_id));
		$grid_site = array();
	
		foreach ($site_list->result() as $row)
		{
			$url_link = $row->url;
				
			if (!parse_url($row->url, PHP_URL_SCHEME)) {
				$url_link = 'http://' . $row->url;
			}
				
			$grid_site[] = '<div class="row"><span><a href="'.$url_link.'">' . $row->url . '</a>' . (isset($row->typename) ? (' (' . $row->typename . ')') : '')
			. ($row->description ? ' - <span class="gdesc">'.$row->description.'</span>' : '')
			. '</span></div>';
		}
	
		if (!$grid_site) {
			return '<center>'.$this->lang->line('site_noresults').'</center>';
		} else {
			return implode('<hr/>', $grid_site);
		}
	}
}

?>